// pages/welfareMall/draw/draw.js
//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //服务器图片的路径片段
    imgUrl:app.globalData.iconImgUrl,
    StatusBar: app.globalData.StatusBar, //状态栏高度
    CustomBar: app.globalData.CustomBar, //头部nav高度
    backIcon: true, // 返回按钮是否显示
    title: "福利商城", //页面标题
    headerClass: "white-header",
    backImg: app.globalData.iconImgUrl+"arrow-left-grey.svg",
    ward: 0,
    rotate: '0deg',
    animation: {},
    animationData: null,
    usableIntegral: 0,
    historyRotate: 0,
    drawing: true,
    //控制
    dis:true,
    times: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.initDraw()
    this.getHonor()
    this.getDrawMes()
  },
  // 奖品类型
  getDrawMes() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'prize/getPrizeImg.do',
      method: 'post',
      success(res) {
        console.log('getdd',res)
        that.setData({
          prizeImgUrl: res.data.prizeImgUrl
        })
        that.getDrawNum()
      }
    })
  },
  // 获取荣誉点
  getHonor() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'mallproduct/getYD.do',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode')
      },
      success(res) {
        console.log('荣誉点', res)
        that.setData({
          usableIntegral: res.data.usableIntegral
        })
      }
    })
  },
  // 奖品
  getDrawNum() {
    const that = this
    wx.request({
      url: app.globalData.baseUrl + 'prize/getPrizeNum',
      method: 'post',
      data: {
        userCode: wx.getStorageSync('userCode')
      },
      success(res) {
        console.log('奖品', res)
        that.setData({
          prizeNum: res.data.prizeNum
        })
      }
    })
  },

  // click(){
  //   this.setData({
  //     　　dis:false//设为方法为无
  //    })
  //    this.draw()
  //   console.log('点击了一次',this.data.dis)
  // },
  // setTime(){
  //   const that = this
  //   console.log('点击了er次',this.data.dis)
  //   setTimeout(function () {
  //     that.setData({
  //       　　dis:true//设为方法为无
  //      })
  //     console.log('定时次',that.data.dis)
  //  }, 1500);//一秒内不能重复点击
  // },

  draw() {
    var that = this
    var drawing = that.data.drawing
    // console.log(wx.getStorageSync('userCode'))
    if (this.data.times !== 1) {
      that.getDrawMes()
      that.animation.rotate(0).step()
    }
    if (drawing) {
      // 防止用户多次点击
      that.setData({
        drawing: false,
        times: 2
      })

      wx.request({
        url: app.globalData.baseUrl + 'prize/getPrizeId.do',
        method: 'post',
        data: {
          userCode: wx.getStorageSync('userCode')
        },
        success(res) {
          console.log('汇总', res)

          if (res.data.errorMsg) {
            wx.showToast({
              title: res.data.errorMsg,
              image: '/assets/images/icon/error.png'
            })
          } else {
            that.setData({
              angle: res.data.prizeInfo.angle,
              PrizeId: res.data.prizeInfo.PrizeId,
              prizeName: res.data.prizeInfo.prizeName
            })
            
            that.animation.rotate(360 * 6 + that.data.angle).step()
            that.setData({
              historyRotate: that.data.historyRotate + 360 * 6 + that.data.angle,
              animation: that.animation.export()
            })
            var timer = null
            timer = setTimeout(function () {
              wx.showModal({
                title: '抽奖提示',
                content: that.data.prizeName
              })
              // 重新开放抽奖点击功能
              that.setData({
                drawing: true
              })
              that.getHonor()
              clearTimeout(timer)
            }, 3000)
            that.getDrawNum()
          }

        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.animation = wx.createAnimation({
      duration: 1500,
      timingFunction: 'ease',
    })
  },

  // 跳转页面
  toPage(e) {
    console.log('eee',e.currentTarget.dataset.url)
    var url = e.currentTarget.dataset.url
    console.log(e.currentTarget.dataset.url)
    wx.navigateTo({
      url: url
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})